<?php


namespace app\task\behavior\course;

use app\common\model\UserCoupon;
use app\task\model\Order as OrderModel;
use think\Db;

class CourseCard
{
    /* @var \app\task\model\Order $model */
    private $model;

    // 小程序id
    private $wxappId;

    /**
     * 执行函数
     * @param $model
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function run($model)
    {
        // 秒杀订单行为管理
        $this->course();
        return true;
    }

    /**
     * 秒杀订单行为管理
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function course()
    {
        // 未支付订单自动关闭
        $this->closeCard();
        $this->closeVideo();
    }

    /**
     * 未支付订单自动关闭
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    private function closeCard()
    {
        $minute = 1;
        // 截止时间
        $deadlineTime = time() - ((int)$minute * 60);
        // 执行自动关闭
        $this->closeOrder($deadlineTime);
        return true;
    }

    public function closeVideo(){
        $minute = 1;
        // 截止时间
        $deadlineTime = time() - ((int)$minute * 60);
        // 执行自动关闭
        $this->VideoOrder($deadlineTime);
        return true;
    }

    private function closeOrder($deadlineTime){
        //未付款
        // 条件
        $filter = array_merge([
            'pay_status' => 10,
            'order_status' => 10,
            'create_time' => ['<=', $deadlineTime]
        ]);
        $list = Db::name('course_order')->where($filter)->select()->toArray();
        $order_ids = array_column($list,'order_id');
        if (!empty($order_ids)){
            foreach ($list as $k=>$v){
                //优惠券返还
                if ($v['coupon_id'] > 0){
                    UserCoupon::setIsUse($v['coupon_id'],false);
                }
            }
            Db::name('course_order')->where(['order_id'=>['in',$order_ids]])->update(['order_status'=>20]);
        }
    }

    private function VideoOrder($deadlineTime){
        //未付款
        // 条件
        $filter = array_merge([
            'pay_status' => 10,
            'create_time' => ['<=', $deadlineTime]
        ]);
        $list = Db::name('video_order')->where($filter)->select()->toArray();
        $order_ids = array_column($list,'order_id');
        if (!empty($order_ids)){
            foreach ($list as $k=>$v){
                //优惠券返还
                if ($v['coupon_id'] > 0){
                    UserCoupon::setIsUse($v['coupon_id'],false);
                }
            }
            Db::name('video_order')->where(['order_id'=>['in',$order_ids]])->update(['pay_status'=>30]);
        }
    }
}